package com.gxan.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxan.common.R;
import com.gxan.pojo.Recruitment;
import com.gxan.service.RecruitmentService;
import com.gxan.service.impl.RecruitmentServiceImpl;
import com.gxan.utils.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addRecruitment")
public class AddRecruitmentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端传过来的参数
        String dept = request.getParameter("dept");
        String recruitment_type = request.getParameter("recruitment_type");
        String recruiting_numbers = request.getParameter("recruiting_numbers");
        String job_title = request.getParameter("job_title");
        String job_code = request.getParameter("job_code");
        String job_classification = request.getParameter("job_classification");
        String registrant = request.getParameter("registrant");
        String record_date = request.getParameter("record_date");
        String expiration_date = request.getParameter("expiration_date");
        String job_description = request.getParameter("job_description");
        String recruitment_description = request.getParameter("recruitment_description");
        //封装对象
        Recruitment recruitment = new Recruitment(null,dept,recruitment_type,
                Integer.valueOf(recruiting_numbers),job_title,job_code,job_classification,
                registrant,DateUtils.string2Date(record_date),DateUtils.string2Date(expiration_date),
                job_description,recruitment_description);
        //传递service处理数据
        RecruitmentService addRecruitmentService =new RecruitmentServiceImpl();
        R r = addRecruitmentService.AddRecruitment(recruitment);
        //响应到前端页面
        //返回给前端需要的R封装的数据，装换成Json方式返回
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
